package com.huike.clues.service.impl;


import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.mapper.TbClueTrackRecordMapper;
import com.huike.clues.service.ITbClueTrackRecordService;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 线索跟进记录Service业务层处理
 *
 * @date 2022-04-22
 */
@Service
public class TbClueTrackRecordServiceImpl implements ITbClueTrackRecordService {
    @Autowired
    private TbClueTrackRecordMapper tbClueTrackRecordMapper;

    @Autowired
    private TbClueMapper tbClueMapper;

    /**
     * 查询线索跟进列表
     *
     * @param clueId
     * @return
     */
    @Override
    public TableDataInfo getList(Long clueId) {
        //需要将结果封装到TableDataInfo实例中
        TableDataInfo tableDataInfo = new TableDataInfo();

        //查询结果为存储TbClueTrackRecord实例的list集合
        List<TbClueTrackRecord> list = tbClueTrackRecordMapper.getList(clueId);

        //封装结果
        tableDataInfo.setTotal(list.size());
        tableDataInfo.setRows(list);
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("查询成功");

        return tableDataInfo;
    }

    /**
     * 新增线索跟进列表
     *
     * @param tbClueTrackRecord
     */
    @Override
    @Transactional //增删改事务管理
    public void add(ClueTrackRecordVo tbClueTrackRecord) {
        //创建TbClueTrackRecord实例，用于封装存储到线索跟进表的数据
        TbClueTrackRecord tbClueTrackRecord1 = new TbClueTrackRecord();
        /*使用工具类复制属性
        TbClue clue = new TbClue();
        BeanUtils.copyBeanProp(clue,tbClueTrackRecord);
        clue.setId(tbClueTrackRecord.getClueId());
        tbClueTrackRecord1.setClueId(tbClueTrackRecord.getClueId());
       BeanUtils.copyBeanProp(tbClueTrackRecord1,tbClueTrackRecord);*/
        tbClueTrackRecord1.setSubject(tbClueTrackRecord.getSubject());
        tbClueTrackRecord1.setRecord(tbClueTrackRecord.getRecord());
        tbClueTrackRecord1.setLevel(tbClueTrackRecord.getLevel());
        tbClueTrackRecord1.setType(tbClueTrackRecord.getType());
        tbClueTrackRecord1.setFalseReason(tbClueTrackRecord.getFalseReason());
        tbClueTrackRecord1.setNextTime(tbClueTrackRecord.getNextTime());
        tbClueTrackRecord1.setCreateBy(SecurityUtils.getUsername());
        tbClueTrackRecord1.setCreateTime(DateUtils.getNowDate());

        //调用mapper层方法新增线索跟进数据
        tbClueTrackRecordMapper.add(tbClueTrackRecord1);

        //因为添加跟进时会有线索信息被更正，因此需要修改线索表中的内容
        tbClueMapper.updateByClueTrackRecordVo(tbClueTrackRecord);

    }
}
